home *** CD-ROM | disk | FTP | other *** search
/ PC Answers 1995 May / PC Answers CD-ROM 7 (Future Publishing) (May 1995).iso / vbits / code / appleman / eventcod.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1994-10-06  |  8.2 KB  |  257 lines

  1. VERSION 2.00
  2. Begin Form Form1 
  3.    Caption         =   "Event Driven Programming Example"
  4.    Height          =   4110
  5.    Left            =   1305
  6.    LinkMode        =   1  'Source
  7.    LinkTopic       =   "Form1"
  8.    ScaleHeight     =   3705
  9.    ScaleWidth      =   4515
  10.    Top             =   1545
  11.    Width           =   4635
  12.    Begin CommandButton CmdVerify 
  13.       Caption         =   "Verify Sort"
  14.       Height          =   495
  15.       Left            =   2160
  16.       TabIndex        =   7
  17.       Top             =   3120
  18.       Width           =   2175
  19.    End
  20.    Begin CommandButton CmdQsortBackground 
  21.       Caption         =   "QuickSort - Background"
  22.       Height          =   495
  23.       Left            =   2160
  24.       TabIndex        =   9
  25.       Top             =   2520
  26.       Width           =   2175
  27.    End
  28.    Begin CommandButton CmdCancelSort 
  29.       Caption         =   "Cancel Sort"
  30.       Height          =   495
  31.       Left            =   840
  32.       TabIndex        =   10
  33.       Top             =   2520
  34.       Width           =   1215
  35.    End
  36.    Begin Timer Timer1 
  37.       Enabled         =   0   'False
  38.       Interval        =   1
  39.       Left            =   240
  40.       Top             =   2400
  41.    End
  42.    Begin CommandButton CmdQsortNoEvents 
  43.       Caption         =   "QuickSort - No Events"
  44.       Height          =   495
  45.       Left            =   2160
  46.       TabIndex        =   8
  47.       Top             =   1920
  48.       Width           =   2175
  49.    End
  50.    Begin CommandButton CmdSrchBackground 
  51.       Caption         =   "SearchEventfully"
  52.       Height          =   495
  53.       Left            =   2160
  54.       TabIndex        =   6
  55.       Top             =   1320
  56.       Width           =   2175
  57.    End
  58.    Begin CommandButton CmdSrchDoEvents 
  59.       Caption         =   "SearchWithDoEvents"
  60.       Height          =   495
  61.       Left            =   2160
  62.       TabIndex        =   3
  63.       Top             =   720
  64.       Width           =   2175
  65.    End
  66.    Begin CommandButton CmdSrchNoEvents 
  67.       Caption         =   "SearchWithoutEvents"
  68.       Height          =   495
  69.       Left            =   2160
  70.       TabIndex        =   2
  71.       Top             =   120
  72.       Width           =   2175
  73.    End
  74.    Begin Label backlabel 
  75.       Alignment       =   2  'Center
  76.       Height          =   255
  77.       Left            =   120
  78.       TabIndex        =   5
  79.       Top             =   1680
  80.       Width           =   1695
  81.    End
  82.    Begin Label Label2 
  83.       Caption         =   "Background Status"
  84.       Height          =   255
  85.       Left            =   120
  86.       TabIndex        =   4
  87.       Top             =   1320
  88.       Width           =   1695
  89.    End
  90.    Begin Label ResultLabel 
  91.       Alignment       =   2  'Center
  92.       Height          =   255
  93.       Left            =   120
  94.       TabIndex        =   0
  95.       Top             =   840
  96.       Width           =   1815
  97.    End
  98.    Begin Label Label1 
  99.       Caption         =   "Search Result"
  100.       Height          =   255
  101.       Left            =   120
  102.       TabIndex        =   1
  103.       Top             =   480
  104.       Width           =   1335
  105.    End
  106. ' EventCod test program.
  107. ' Copyright (c) 1992, by Desaware
  108. ' Indicates the operation of timer 1 on this form.
  109. ' 0 indicates no operation
  110. ' 1 indicates initializing is in progress
  111. ' 2 indicates SearchEventfully is in progress
  112. ' 3 indicates Background sorting is in progress
  113. Dim TimerMode%
  114. Sub CmdCancelSort_Click ()
  115.     t% = QSortBackground%(-1, 0)
  116. End Sub
  117. Sub CmdQsortBackground_Click ()
  118.     Dim t%
  119.     ' The following line will prevent reentry - Refer
  120.     ' to the article for alternate approaches such
  121.     ' as starting a new sort.
  122.     If TimerMode% Then
  123.         MsgBox "Background operation already in progress"
  124.         Exit Sub
  125.         End If
  126.     t% = QSortBackground%(1, ArraySize%)
  127.     If t% < 0 Then ' Start event driven sorting
  128.         backlabel.Caption = "Sorting"
  129.         CmdCancelSort.Enabled = -1
  130.         TimerMode% = 3
  131.         Timer1.Enabled = -1
  132.     Else
  133.         If t% = 0 Then
  134.             ResultLabel.Caption = "Sorting complete"
  135.         Else
  136.             ResultLabel.Caption = "Sorting cancelled"
  137.             End If
  138.         End If
  139. End Sub
  140. '   Perform a QuickSort without events
  141. Sub CmdQsortNoEvents_Click ()
  142. Dim oldmouseptr%
  143.     oldmouseptr% = Screen.MousePointer
  144.     Screen.MousePointer = 11
  145.     QSortNoEvents 1, ArraySize
  146.     Screen.MousePointer = oldmouseptr%
  147. End Sub
  148. ' Search "Eventfully" - i.e. background processing
  149. Sub CmdSrchBackground_Click ()
  150.     Dim r, t%
  151.     ' The following line will prevent reentry - Refer
  152.     ' to the article for alternate approaches such
  153.     ' as starting a new search.
  154.     If TimerMode% = 3 Then
  155.         MsgBox "Background sort in progress"
  156.         Exit Sub
  157.         End If
  158.     ' Ignore command if any other background operation is
  159.     ' in progress
  160.     If TimerMode% Then Exit Sub
  161.     r = Int(Rnd * ArraySize + 1)
  162.     t% = SearchEventfully%(r, -1)
  163.     If t% < 0 Then ' Start event driven initialization
  164.         backlabel.Caption = "Searching"
  165.         TimerMode% = 2
  166.         Timer1.Enabled = -1
  167.     Else ResultLabel.Caption = Str$(t%)
  168.     End If
  169. End Sub
  170. '   Search with DoEvents command
  171. Sub CmdSrchDoEvents_Click ()
  172.     Dim r
  173.     r = Int(Rnd * ArraySize + 1)
  174.     ResultLabel.Caption = Str$(SearchWithDoEvents%(r))
  175. End Sub
  176. ' Search without DoEvents command
  177. Sub CmdSrchNoEvents_Click ()
  178.     Dim r
  179.     r = Int(Rnd * ArraySize + 1)
  180.     ResultLabel.Caption = Str$(SearchWithoutEvents%(r))
  181. End Sub
  182. Sub CmdVerify_Click ()
  183. Dim x%
  184. Dim oldmouseptr%
  185.     oldmouseptr% = Screen.MousePointer
  186.     Screen.MousePointer = 11
  187.     For x% = 1 To ArraySize% - 1
  188.         If SampleArray(x%) > SampleArray(x% + 1) Then
  189.             ResultLabel.Caption = "Not Sorted @" + Str$(x%)
  190.             Screen.MousePointer = oldmouseptr%
  191.             Exit Sub
  192.             End If
  193.         Next x%
  194.     ResultLabel.Caption = "Sorted"
  195.     Screen.MousePointer = oldmouseptr%
  196. End Sub
  197. Sub Form_Load ()
  198.     Dim t%
  199.     Randomize
  200.     t% = LoadSampleArray()
  201.     If t% Then ' Start event driven initialization
  202.         ' Don't allow sorting during initialization!
  203.         CmdQsortNoEvents.Enabled = 0
  204.         CmdQsortBackground.Enabled = 0
  205.         ' The cancel sort command is disabled to start
  206.         CmdCancelSort.Enabled = 0
  207.         backlabel.Caption = "Initializing"
  208.         TimerMode% = 1
  209.         Timer1.Enabled = -1
  210.     End If
  211. End Sub
  212. '   We use the timer to keep background operations
  213. '   operating properly.
  214. '   This algorithm allows only one background operation
  215. '   at a time.  See article text for ideas how maintaining
  216. '   multiple concurrent operations.
  217. Sub Timer1_Timer ()
  218.     Dim t%, u%
  219.     Select Case TimerMode%
  220.         Case 1  ' Background initialization
  221.                 t% = LoadSampleArray%()
  222.                 If Not t% Then  ' We're done!
  223.                     TimerMode% = 0
  224.                     Timer1.Enabled = 0
  225.                     backlabel.Caption = "Idle"
  226.                     CmdQsortNoEvents.Enabled = -1
  227.                     CmdQsortBackground.Enabled = -1
  228.                 End If
  229.         Case 2  ' Background searching
  230.                 t% = SearchEventfully%(0, 0)
  231.                 If t% >= 0 Then ' We're done!
  232.                     TimerMode% = 0
  233.                     Timer1.Enabled = 0
  234.                     ResultLabel.Caption = Str$(t%)
  235.                     backlabel.Caption = "Idle"
  236.                 End If
  237.         Case 3  ' Background sorting
  238.                 ' The sort granularity is set here
  239.                 For u% = 1 To 20
  240.                     t% = QSortBackground%(0, 0)
  241.                     If t% >= 0 Then Exit For
  242.                     Next u%
  243.                 If t% >= 0 Then ' We're done!
  244.                     CmdCancelSort.Enabled = 0
  245.                     TimerMode% = 0
  246.                     Timer1.Enabled = 0
  247.                     backlabel.Caption = "Idle"
  248.                     If t% = 0 Then
  249.                         ResultLabel.Caption = "Sorting complete"
  250.                     Else
  251.                         ResultLabel.Caption = "Sorting cancelled"
  252.                         End If
  253.                     End If
  254.     End Select
  255.         
  256. End Sub
  257.